// miniprogram/pages/admin/cos/cos.js
var Charts = require('wxcharts-min.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
      title: '正在计算',
      size: '...'
    }],
    colors:  ['#9e8eff','#ffaa4b', '#eee'],
  },
  chart(series) {

    new Charts({
      canvasId: 'pieCanvas',
      type: 'pie',
      series: series,
      //  [{
      //     name: '成交量1',
      //     data: 15,
      // }, {
      //     name: '成交量2',
      //     data: 35,
      // }, {
      //     name: '成交量3',
      //     data: 78,
      // }, {
      //     name: '成交量4',
      //     data: 63,
      // }],
      width: app.globalData.systemInfo.windowWidth * 0.85,
      height: app.globalData.systemInfo.windowWidth * 0.85,
      dataLabel: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.showNavigationBarLoading();
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      this.setData({
        user:user
      })
      wx.cloud.callFunction({
        name: 'storage',
        data: {
          studioid: user.studioid
        },
        success: res => {
          wx.hideNavigationBarLoading();
          let items = res.result;
          console.log(items)
     
        //   that.chart(res.result)
          let leftCOS = (user.studio.cos||50)*1024*1024*1024
          items.map((v, i) => {
            leftCOS -=v.size
            v.data = v.size
            v.size = that.unitConversion(v.size);
            v.name = v.title
            v.color = that.data.colors[i]
           
          })
          let leftCOSItem = {
            title:'可用空间',
            name:'可用空间',
            data:leftCOS,
            size:that.unitConversion(leftCOS),
            color: that.data.colors[items.length]
          }
          items.push(leftCOSItem)
          that.setData({
            items: items,
            used:that.unitConversion(user.studio.cos*1024*1024*1024-leftCOS)
          });
          that.chart(items)
        }
      })
    })
  },

  unitConversion: function (size) {
    const unit = [" B", " K", " M", " G", " T"];
    let formResult = "";
    for (let j = 0; j < unit.length; j++) {
      if (size < 1024) {
        formResult = Math.ceil(size) + unit[j];
        break;
      }
      size = size / 1024.0;
    }
    return formResult;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})